package com.brainardphotography.blog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Embedded;
import javax.persistence.Id;
import javax.persistence.PrePersist;

public class BlogEntry implements Serializable {
	private static final long serialVersionUID = 557275417906514613L;

	@Id
	private String id;

	private String title;
	private String teaser;
	private String text;
	private String imageURL;
	private String authorID;
	private boolean skipHomePage;
	private Date created;
	private Date lastModified;

	@Embedded
	private List<Comment> comments = new ArrayList<Comment>();

	public BlogEntry() {
		// Nothing really to do here...
	}

	@SuppressWarnings("unused")
	// This method is called by Objectify.
	@PrePersist
	private void prePersist() {
		Date now = new Date();

		if (created == null)
			created = now;

		lastModified = now;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAuthorID() {
		return authorID;
	}

	public void setAuthor(String authorID) {
		this.authorID = authorID;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTeaser() {
		return teaser;
	}

	public void setTeaser(String teaser) {
		this.teaser = teaser;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getImageURL() {
		return imageURL;
	}

	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}

	public boolean isSkipHomePage() {
		return skipHomePage;
	}
	
	public boolean getSkipHomePage() {
		return skipHomePage;
	}

	public void setSkipHomePage(boolean skipHomePage) {
		this.skipHomePage = skipHomePage;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getLastModified() {
		return lastModified;
	}

	public void setLastModified(Date lastModified) {
		this.lastModified = lastModified;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public static Builder withBuilder() {
		return new Builder(new BlogEntry());
	}

	public static class Builder {
		private BlogEntry entry = null;

		private Builder(BlogEntry entry) {
			this.entry = entry;
		}

		public Builder author(String authorID) {
			entry.authorID = authorID;
			return this;
		}

		public Builder title(String title) {
			entry.title = title;
			return this;
		}

		public Builder teaser(String teaser) {
			entry.teaser = teaser;
			return this;
		}

		public Builder text(String text) {
			entry.text = text;
			return this;
		}

		public Builder imageURL(String imageURL) {
			entry.imageURL = imageURL;
			return this;
		}

		public Builder skipHomePage(boolean skipHomePage) {
			entry.skipHomePage = skipHomePage;
			return this;
		}

		public Builder created(Date created) {
			entry.created = created;
			return this;
		}

		public Builder lastModified(Date lastModified) {
			entry.lastModified = lastModified;
			return this;
		}

		public BlogEntry build() {
			return this.entry;
		}
	}

	public String toString() {
		StringBuilder b = new StringBuilder();

		b.append(getClass().getName()).append("[").append("id: ").append(id)
				.append(",authorID: ").append(authorID).append(",title: ")
				.append(title).append(",teaser: ").append(teaser)
				.append(",text: ").append(text).append(",created: ")
				.append(",imageURL: ").append(imageURL).append(created)
				.append(",skipHomePage: ").append(skipHomePage)
				.append("]");

		return b.toString();
	}
}
